package com.tsd.jxc.goods.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.common.controller.BaseController;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.core.vo.PageBean;
import com.tsd.jxc.goods.entity.JxcCategory;
import com.tsd.jxc.goods.entity.JxcCategoryExt;
import com.tsd.jxc.goods.service.JxcCategoryService;
import com.tsd.system.entity.SysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：JxcCategory商品类目表 控制层
 *
 * @author Hillpool
 * @date 2022/07/29 15:02:35
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
@Api(tags = "进销存-商品类目表", description = "商品类目表的API描述")
public class JxcCategoryController extends BaseController {

    @Resource
    private JxcCategoryService jxcCategoryService;

    @ApiOperation(value = "根据sid获取商品类目表", notes = "根据sid获取商品类目表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sid", value = "商品类目表sid", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/getJxcCategoryBySid.action", method = RequestMethod.POST)
    public HttpResult<JxcCategory> getJxcCategoryBySid() {
        HttpResult<JxcCategory> hr = new HttpResult<JxcCategory>();
        try {
            Map<String, Object> map = super.getParams(request);
            String sid = HlpUtils.getString(map, "sid");
            super.checkEmpty(sid, "请提供参数sid");
            JxcCategory jxcCategory = jxcCategoryService.queryBySid(sid);
            hr.setData(jxcCategory);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "根据id删除商品类目表", notes = "根据id删除商品类目表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sids", value = "商品类目表sids", required = true, dataType = "string", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/deleteJxcCategorys.action", method = RequestMethod.POST)
    public HttpResult<String> deleteJxcCategorys() {
        HttpResult<String> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Object sids = map.get("sids");
            super.checkEmpty(sids, "请提供参数sids");
            List<String> list = JSON.parseArray(JSON.toJSONString(sids), String.class);
            jxcCategoryService.deleteJxcCategorys(list, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ApiOperation(value = "保存商品类目表", notes = "保存商品类目表，具有id值则更新，没有id值则新增")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "obj", value = "商品类目表", required = true, paramType = "body", dataType = "JxcCategory")
    })
    @ResponseBody
    @RequestMapping(value = "/saveJxcCategory.action", method = RequestMethod.POST)
    public HttpResult<Object> saveJxcCategory() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            String json = JSON.toJSONString(map);
            JxcCategory t = JSON.parseObject(json, JxcCategory.class);
            jxcCategoryService.saveJxcCategory(t, opUser);
            hr.setData(t);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    @ApiOperation(value = "分页查询商品类目表", notes = "分页查询商品类目表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "name", value = "名称", required = false, dataType = "string", paramType = "form"),
            @ApiImplicitParam(name = "pageNo", value = "页码", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "pageSize", value = "页大小，每页记录数", required = false, dataType = "int", paramType = "form"),
            @ApiImplicitParam(name = "sorts", value = "排序设置，方式['property1 desc','property2']", required = false, dataType = "String", paramType = "form"),
            @ApiImplicitParam(name = "totalRows", value = "总记录数，为了减少统计总记录数，一般从第二页开始前端提供totalRows", required = false, dataType = "int", paramType = "form")
    })
    @ResponseBody
    @RequestMapping(value = "/findJxcCategorys.action", method = RequestMethod.POST)
    public HttpResult<Object> findJxcCategorys() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "code", "code", false, false, false);
            HlpUtils.copyStringValue(map, params, "name", "name", false, false, false);
            HlpUtils.copyStringValue(map, params, "parent_sid", "parent_sid", false, false, false);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            PageBean page = null;
            List<JxcCategoryExt> list = null;
            if (map.containsKey("nopage")) {
                list = jxcCategoryService.findJxcCategorys(params);
            } else {
                page = new PageBean(map);
                list = jxcCategoryService.findJxcCategorys(params, page);
            }
            hr.setData(list);
            hr.setPage(page);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    @ResponseBody
    @RequestMapping(value = "/findJxcCategoryTree.action", method = RequestMethod.POST)
    public HttpResult<Object> findJxcCategoryTree() {
        HttpResult<Object> hr = new HttpResult<>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<>(8);
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            HlpUtils.copyStringValue(map, params, "code", "code", false, false, false);
            HlpUtils.copyStringValue(map, params, "name", "name", false, false, false);
            HlpUtils.copyStringValue(map, params, "search_value", "search_value", false, true);
            List<JxcCategoryExt> list = jxcCategoryService.findJxcCategoryTree(params);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


}
